package com.atguigu.spzx.user.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.spzx.common.utils.AuthContextUtil;
import com.atguigu.spzx.model.dto.user.UserLoginDto;
import com.atguigu.spzx.model.dto.user.UserRegisterDto;
import com.atguigu.spzx.model.entity.user.UserInfo;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.user.UserInfoVo;
import com.atguigu.spzx.user.service.UserInfoService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user/userInfo/")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterDto userRegisterDto){
        userInfoService.register(userRegisterDto);
        return Result.ok(null);
    }

    @PostMapping("/login")
    public Result login(@RequestBody UserLoginDto userLoginDto, HttpServletRequest request){
        String ip = request.getRemoteAddr();
        String token = userInfoService.login(userLoginDto,ip);
        return Result.ok(token);
    }


    @GetMapping("/auth/checkUserInfo/{token}")
    public UserInfo checkUserInfo(HttpServletRequest request,@PathVariable String token){
        //String token = request.getHeader("token");
        UserInfo userInfo = userInfoService.checkUserInfo(token);
        return userInfo;
    }


    @GetMapping("/auth/getCurrentUserInfo")
    public Result<UserInfoVo> getCurrentUserInfo(HttpServletRequest request) {
        UserInfo userInfo = AuthContextUtil.getUserInfo();
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setAvatar(userInfo.getAvatar());
        userInfoVo.setNickName(userInfo.getNickName());
        return Result.ok(userInfoVo);
    }



}
